﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Entity.Entities;

namespace LY.DataAccess.Repository
{
    public class RoleRepository : BaseRepository
    {
        /// <summary>
        /// 删除当前角色下的菜单权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public int DeleteRole(int roleId)
        {
            try
            {
                string SQLS = @"delete from SYS_ROLE_RIGHT where role_Id=" + roleId;
                return base.Excute(SQLS, null);
            }
            catch (Exception)
            {
                return -1;
                throw;
            }

        }

        public int DeleteS_Role(int id)
        {
            try
            {
                string SQLS = @"delete from [S_Role] where ID=" + id;
                return base.Excute(SQLS, null);
            }
            catch (Exception)
            {
                return -1;
                throw;
            }
        }

        public PagedList<S_Role> QueryRoleList(RoleSearch search, PageView view)
        {
            string cols = @" [ID]
                          ,[Name]
                          ,[Code]
                          ,[TypeID]
                          ,[Flag]
                          ,[UnitID]
                          ,[Sequence]
                          ,[Authorization]
                          ,[Remark] ";
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.Name))
            {
                sqlWhere += " AND [Name] LIKE '%" + search.Name + "%' ";
            }

            return base.PageGet<S_Role>(view, cols,
                @" [S_Role] "
                , sqlWhere
                , " [ID] ASC ",
                "");
        }

        public S_Role GetRole(int? id)
        {
            string sql = @" SELECT [ID]
                          ,[Name]
                          ,[Code]
                          ,[TypeID]
                          ,[Flag]
                          ,[UnitID]
                          ,[Sequence]
                          ,[Authorization]
                          ,[Remark]
                       FROM [S_Role] WHERE ID=@ID";
            return base.Get<S_Role>(sql, new { ID = id });
        }

        public int GiveAuthorization(string Authorization, int? id)
        {


            try
            {
                string delSQLS = @"delete from  S_ModuleAccess where UserRoleID = " + id.ToString();
                base.Excute(delSQLS, null);
                string[] aa = Authorization.Split(',');
                for (int i = 0; i < aa.Count(); i++)
                {
                    string SQLS = @"insert into S_ModuleAccess(UserRoleID,ModuleID)values(" + id.ToString() + "," + aa[i].ToString() + ")";
                    base.Excute(SQLS, null);
                }

//                string sql = @" UPDATE [S_Role] 
//                            SET [Authorization]=@Authorization 
//                            WHERE ID=@ID ";
//                return base.Get<int>(sql, new { Authorization = Authorization, ID = id });
                return 1;

            }
            catch (Exception)
            {
                return -1;
                throw;
            }


        }

        public int SaveRoleUser(string Users, int? id)
        {


            try
            {
                string delSQLS = @"delete from  [S_RoleUser] where RoleID = " + id.ToString();
                base.Excute(delSQLS, null);
                string[] aa = Users.Split(',');
                for (int i = 0; i < aa.Count(); i++)
                {
                    string SQLS = @"insert into [S_RoleUser](RoleID,UserID)values(" + id.ToString() + "," + aa[i].ToString() + ")";
                    base.Excute(SQLS, null);
                }

//                string sql = @" UPDATE [S_Role] 
//                            SET [Authorization]=@Authorization 
//                            WHERE ID=@ID ";
//                return base.Get<int>(sql, new { Authorization = Authorization, ID = id });
                return 1;

            }
            catch (Exception)
            {
                return -1;
                throw;
            }


        }

        public List<S_Role> GetRoleList(int? ID)
        {
            string sql = @" SELECT [ID]
                          ,[Name]
                          ,[Code]
                          ,[TypeID]
                          ,[Flag]
                          ,[UnitID]
                          ,[Sequence]
                          ,[Authorization]
                          ,[Remark]
                       FROM [S_Role] WHERE [ID]=@ID";
            return base.Query<S_Role>(sql, new { ID = ID });
        }

        public List<S_Role> GetAllRoleList()
        {
            string sql = @" SELECT [ID]
                          ,[Name]
                          ,[Code]
                          ,[TypeID]
                          ,[Flag]
                          ,[UnitID]
                          ,[Sequence]
                          ,[Authorization]
                          ,[Remark]
                       FROM [S_Role] ";
            return base.Query<S_Role>(sql, null);
        }

        public List<S_RoleUser> GetRoleUserList(int? RoleID)
        {
            string sql = @" SELECT *
                       FROM [S_RoleUser] WHERE [RoleID]=@RoleID";
            return base.Query<S_RoleUser>(sql, new { RoleID = RoleID });
        }


    }
}
